package com.rainng.coursesystem.service;

import cn.hutool.core.bean.BeanUtil;
import com.rainng.coursesystem.manager.LoginStatusManager;
import com.rainng.coursesystem.manager.UserManager;
import com.rainng.coursesystem.model.bo.AuthInfoBO;
import com.rainng.coursesystem.model.bo.LoginStatusBO;
import com.rainng.coursesystem.model.constant.UserType;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import com.rainng.coursesystem.util.Md5Encrypt;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Service
@Slf4j
@Data
public class UserService extends BaseService {
    private static final String PASSWORD_SALT = "_Rain_Ng-_Azure_99";

    private final HttpSession session;
    private final UserManager manager;
    private final LoginStatusManager loginStatusManager;
    private final Md5Encrypt md5Encrypt;


    @Value("${envi.name}")
    private String envi;


    public UserService(HttpSession session, UserManager manager, LoginStatusManager loginStatusManager, Md5Encrypt md5Encrypt) {
        this.session = session;
        this.manager = manager;
        this.loginStatusManager = loginStatusManager;
        this.md5Encrypt = md5Encrypt;
    }

    public ResultVO login(String username, String password, Integer userType) {
        AuthInfoBO authInfo = manager.getAuthInfoByUsername(username, userType);
        if (authInfo == null) {
            return failedResult("用户不存在");
        }
//        String passwordHash = computePasswordHash(password);
        if (!password.equals(authInfo.getPassword())) {
            return failedResult("密码错误");
        }

        if (authInfo.getUserType().equals(UserType.STUDENT)) {
            manager.updateStudentLastLoginTime(username);
        }

        LoginStatusBO statusBO = LoginStatusBO.fromAuthInfo(authInfo);
        loginStatusManager.setLoginStatus(session, statusBO);

        return result(statusBO);
    }


    //2023封装多种角色
    public ResultVO loginValid(String username, String password) {
        AuthInfoBO authInfo = manager.getAuthInfoByUsernameWithoutUserType(username);
        if (authInfo == null) {
            return failedResult("用户不存在");
        }
//        String passwordHash = computePasswordHash(password);
        if (!password.equals(authInfo.getPassword())) {
            return failedResult("密码错误");
        }

//        if (authInfo.getUserType().equals(UserType.STUDENT)) {
//            manager.updateStudentLastLoginTime(username);
//        }

        LoginStatusBO statusBO = LoginStatusBO.fromAuthInfo(authInfo);
        String cc="";
        loginStatusManager.setLoginStatus(session, statusBO);

        return result(statusBO);
    }


    public ResultVO getLoginStatus() {
        LoginStatusBO statusBO = loginStatusManager.getLoginStatus(session);
        if ("dev".equals(envi)) {
            log.info(envi);
            LoginStatusBO kuayu = new LoginStatusBO();
            kuayu.setUserId(1);
            kuayu.setLoggedIn(true);
            kuayu.setPermission(255);
            kuayu.setUserType(3);
            statusBO = kuayu;
        }
        return result(statusBO);
    }


    public ResultVO getLoginStatusWithDepartment() {
        LoginStatusBO statusBO = loginStatusManager.getLoginStatus(session);
        if ("dev".equals(envi)) {
            log.info(envi);
            LoginStatusBO kuayu = new LoginStatusBO();
            kuayu.setUserId(1);
            kuayu.setLoggedIn(true);
            kuayu.setPermission(255);
            kuayu.setUserType(3);
            statusBO = kuayu;
        }

        //转map
        Map<String, Object> map = BeanUtil.beanToMap(statusBO);
        if (map.get("username") == null) {
            map.put("groupName", null);
            map.put("roleName", null);
            map.put("groupId", null);
            map.put("uId", null);
            return result(map);
        }

        String name = map.get("username").toString();
        //根据name拿department
        String res = manager.getDepartNameByUserName(name);
        String groupId = manager.getDepartIdByUserName(name);//待优化
        String uId = manager.getUserIdByUserName(name);
        map.put("uId", uId);
        map.put("groupName", res);
        map.put("groupId", groupId);
        String userType = map.get("userType").toString();
        if ("1".equals(userType)) {
            map.put("roleName", "主检");
        }
        if ("2".equals(userType)) {
            map.put("roleName", "组长");
        }
        if ("3".equals(userType)) {
            map.put("roleName", "超级管理员");
        }
        if ("4".equals(userType)) {
            map.put("roleName", "领导");
        }
        if ("5".equals(userType)) {
            map.put("roleName", "设备管理员");
        }
        if ("6".equals(userType)) {
            map.put("roleName", "场地管理员");
        }
        if ("7".equals(userType)) {
            map.put("roleName", "企业");
        }
        if ("8".equals(userType)) {
            map.put("roleName", "试验员");
        }
        return result(map);
    }

    public ResultVO logout() {
        loginStatusManager.setLoginStatus(session, null);
        return result("注销成功");
    }

    public String computePasswordHash(String password) {
        String md5 = md5Encrypt.computeHexString(password);
        return md5Encrypt.computeHexString(md5 + PASSWORD_SALT);
    }
}
